package com.tool.xmltool.bridge;

import cn.hutool.core.collection.CollUtil;
import com.tool.xmltool.entry.ComXmlConfigDO;
import com.tool.xmltool.gateway.impl.ComXmlConfigGatewayImpl;

import java.util.List;

/**
 * 基础配置获取实现
 *
 * @author: zhoubin
 */
public class DbConfigSource implements ConfigSource {

    ComXmlConfigGatewayImpl configGateway;

    public DbConfigSource() {
        configGateway = new ComXmlConfigGatewayImpl();
    }

    @Override
    public List<ComXmlConfigDO> findXmlConfigByMsgType(String msgType) {
        // 根据报文类型查询配置，返回来的对象的父节点都为空
        List<ComXmlConfigDO> xmlConfig = configGateway.findXmlConfigByMsgType(msgType);

        fillAppendList(xmlConfig);
        return xmlConfig;
    }

    /**
     * 将配置节点的循环域内容填充完整.
     *
     * @param configList 配置列表
     */
    private void fillAppendList(List<ComXmlConfigDO> configList) {
        if (CollUtil.isEmpty(configList)) {
            return;
        }

        for (ComXmlConfigDO config : configList) {

            if (config.getCyclicFieldFlag()) {
                List<ComXmlConfigDO> sonConfigList = configGateway.findByParentPath(config.getMsgType(), config.getBusinessSemantics());
                config.setAppendList(sonConfigList);

                // 子节点列表也要填充其自己的子节点
                fillAppendList(sonConfigList);
            }
        }
    }
}
